$(document).ready(function(){
    userCommand();
    activeUser();
    updateUserInfo();
    registerShop();
});

function userCommand(){
    $(document.body).on('click', '.login-btn', function(){
        var form = $(this).closest('form');
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            cache: false,
            data: form.serialize(),
            beforeSend: function(){
                $('.pprogress').slideDown();
                form.find('button.btn').attr('disabled', 'disabled');
                form.find('input').attr('readonly', 'readonly');
            },
            success: function(data){
                $('.pprogress').slideUp();
                var msg = "<p>" + data.message + "</p>";
                if(data.code==1){
                    $('#login-message').removeClass('alert-danger').addClass('alert-success');
                } else {
                    $('#login-message').removeClass('alert-success').addClass('alert-danger');
                    msg = msg + "<ul>"+data.details+"</ul>";
                }
                $('#login-message').removeClass('hidden').html(msg);

                if(data.code==1){
                    setTimeout(function (){window.location = data.href;}, 500);
                } else {
                    form.find('button.btn').removeAttr('disabled');
                    form.find('input').removeAttr('readonly');
                }
            }
        });
    });

    $(document.body).on('click', '#signup-btn', function(){
        var form = $(this).closest('form');
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            cache: false,
            data: form.serialize(),
            beforeSend: function(){
                $('.pprogress').slideDown();
                form.find('button.btn').attr('disabled', 'disabled');
                form.find('input').attr('readonly', 'readonly');
            },
            success: function(data){
                $('.pprogress').slideUp();
                if(data.code==0){
                    $('#signup-message').removeClass('alert-success').addClass('alert-danger');
                    form.find('button.btn').removeAttr('disabled');
                    form.find('input').removeAttr('readonly');
                } else {
                    form.find('.btn-close-modal').removeAttr('disabled');
                    $('#signup-message').removeClass('alert-danger').addClass('alert-success');
                }
                var msg = "<p>" + data.message + "</p>";
                if(data.details!=undefined){
                    msg += "<ul>"+data.details+"</ul>";
                }
                $('#signup-message').removeClass('hidden').html(msg);
                htmlScrollTo('#signup-message');
            }
        });
    });

    $(document.body).on('click', '#forget-password-btn', function(){
        var form = $(this).closest('form');
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            cache: false,
            data: form.serialize(),
            beforeSend: function(){
                $('.pprogress').slideDown();
                form.find('button.btn').attr('disabled', 'disabled');
                form.find('input').attr('readonly', 'readonly');
            },
            success: function(data){
                $('.pprogress').slideUp();
                if(data.code==0){
                    $('#forget-password-message').removeClass('alert-success').addClass('alert-danger');
                    form.find('button.btn').removeAttr('disabled');
                    form.find('input').removeAttr('readonly');
                } else {
                    form.find('.btn-close-modal').removeAttr('disabled');
                    $('#forget-password-message').removeClass('alert-danger').addClass('alert-success');
                }
                var msg = "<p>" + data.message + "</p>";
                if(data.details!=undefined){
                    msg += "<ul>"+data.details+"</ul>";
                }
                $('#forget-password-message').removeClass('hidden').html(msg);
            }
        });
    });

    $(document.body).on('click', '#reset-btn', function(){
        var form = $(this).closest('form');
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            cache: false,
            data: form.serialize(),
            beforeSend: function(){
                $('.pprogress').slideDown();
                form.find('button.btn').attr('disabled', 'disabled');
                form.find('input').attr('readonly', 'readonly');
            },
            success: function(data){
                $('.pprogress').slideUp();
                if(data.code==0){
                    $('#reset-message').removeClass('alert-success').addClass('alert-danger');
                    form.find('button.btn').removeAttr('disabled');
                    form.find('input').removeAttr('readonly');
                } else {
                    form.find('.btn-close-modal').removeAttr('disabled');
                    $('#reset-message').removeClass('alert-danger').addClass('alert-success');
                }
                var msg = "<p>" + data.message + "</p>";
                if(data.details!=undefined){
                    msg += "<ul>"+data.details+"</ul>";
                }
                $('#reset-message').removeClass('hidden').html(msg);
            }
        });
    });

    $(document.body).on('click', '.btn-close-modal', function(){
        $(this).closest('form').find('button.btn').removeAttr('disabled');
        $(this).closest('form').find('input').removeAttr('readonly');
    });
}

function activeUser(){
    $('#active-btn').click(function(){
        var form = $(this).closest('form');
        $.ajax({
            url:form.attr('action'),
            type:form.attr('method'),
            data:form.serialize(),
            cache:false,
            success: function(data){
                var html = "<h4>" + data.message + "</h4>";
                $('div#message').removeClass('hidden');
                if(data.code==0){
                    html += "<ul>" + data.details + "</ul>";
                    $('div#message').removeClass('alert-success').addClass('alert-danger').html(html);
                } else {
                    $('div#message').removeClass('alert-danger').addClass('alert-success').html(html);
                    $('#active-success').removeClass('hidden');
                    setTimeout(function(){window.location = data.href;}, 3000);
                }
            }
        }).done(function(){
            htmlScrollToTop();
        });
    });
}

function updateUserInfo(){
    $('#user-update-btn, #user-shop-update-btn').click(function(){
        flushCKEditor();
        $('#modal-progress').modal();
        var form = $(this).closest('form');
        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            data: form.serialize(),
            cache: false,
            success: function(data){
                $('div#flg').html($('#template-alert div.alert').clone());
                var msg = '';
                if(data.title!=undefined){
                    msg = "<h4>" + data.title + "</h4>";
                }
                if(data.code == 0){
                    if(data.text!=undefined){
                        msg += "<div>" + data.text + "</div>";
                    }
                    $('div#flg div.alert').removeClass('alert-success').addClass('alert-danger');
                } else {
                    $('div#flg div.alert').addClass('alert-success').removeClass('alert-danger');
                }
                $('div#flg div.alert div#msg').html(msg);
            }
        }).done(function(){
            htmlScrollToTop();
            $('#modal-progress').modal('hide');
        });
    });
}

function registerShop(){
    if(typeof submitUpdateForm == 'function'){
        submitUpdateForm('.btn-submit-update', evtAfterRegisterShop);
    }
}

function evtAfterRegisterShop(){}